package com.czk.service.impl;

import com.czk.service.HiveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/9/5 16:41
 */
@Service
@Slf4j
public class HiveServiceImpl implements HiveService {
    @Autowired
    @Qualifier("hiveJdbcTemplate")
    private JdbcTemplate hiveJdbcTemplate;

    @Autowired
    @Qualifier("hiveDruidDataSource")
    private DataSource hiveDruidDataSource;

    @Override
    public Object select(String hql) {
        return hiveJdbcTemplate.queryForObject(hql, Object.class);
    }

    @Override
    public List<String> listAllTables() {
        // 创建集合
        List<String> result = new ArrayList<>();
        //
        String sql = "show tables";
        // 拿到执行声明
        Statement statement = null;
        try {
            statement = hiveDruidDataSource.getConnection().createStatement();
            // 执行
            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                result.add(resultSet.getString(1));
            }
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public List<String> describeTable(String tableName) {
        if (tableName == null) {
            log.error("表名不能为空");
            return null;
        }
        List<String> list = new ArrayList<>();
        try {
            Statement statement = hiveDruidDataSource.getConnection().createStatement();
            String sql = "describe formatted " + tableName;
            // 执行
            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                list.add(resultSet.getString(1));
            }
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }

    @Override
    public List<String> selectFromTable(String tableName) {
        if (tableName == null) {
            log.error("表名不能为空");
            return null;
        }
        List<String> list = new ArrayList<>();
        try {
            Statement statement = hiveDruidDataSource.getConnection().createStatement();
            String sql = "select * from " + tableName;
            ResultSet resultSet = statement.executeQuery(sql);
            // 拿到列数
            int columnCount = resultSet.getMetaData().getColumnCount();
            String str = null;

            while (resultSet.next()) {
                str = "";
                for (int i = 0; i < columnCount; i++) {
                    str += resultSet.getString(i) + " ";
                }
                str += resultSet.getString(columnCount);
                list.add(str);
            }
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    @Override
    public void createTable(String tableName) {
        String hql = "show tables";
        Statement statement = null;
        try {
            statement = hiveDruidDataSource.getConnection().createStatement();
            // 执行创建
            ResultSet resultSet = statement.executeQuery(hql);
            while (resultSet.next()){
                System.out.println(resultSet.getString(1));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
